// pages/others/setting/setting.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      url:'',
      img:'',
      info:[],
      name:'',
      modalName3:'',
      modalName5:'',
      id:'',
      bgimg:'',
      haveImg: app.globalData.haveImg,
      bgimg1: app.globalData.bgimg,
      NAME:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    var info=JSON.parse(options.info)
    this.data.info=info
    this.setData({
      name:info.name,
      bgimg1: app.globalData.bgimg,
      haveImg: app.globalData.haveImg,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  updatePic:function()
  {
    var that=this
    wx.chooseImage({
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // app.globalData.backgroundPic = tempFilePaths[0]
        wx.uploadFile({
          url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/fileInfo/upload',
          filePath: tempFilePaths[0],
          header: {
            "Content-Type": "multipart/form-data"
          },
          name: 'myFile',
          success(res) {
            var data = JSON.parse(res.data)
            console.log(data)
            //得到url
            var url = data.url
            that.data.img=url
            that.setData({
              url:url,
            })
          }
        })
      },
    })
  },
  
  inviteMember:function(e)
  {
    this.setData({
      modalName3:'DialogModal3',
    })

  },
  hideModal: function () {
    this.setData({
      modalName3: '',
      modalName5: '',
      notice: ''
    })
  },
  updataMember:function(e)
  {
    console.log(e.detail.value)
    this.id = e.detail.value
  },
  update3:function(e)
  {
  this.setData({
    modalName3: ''
  })
  console.log(app.globalData.userInfo.token)
  console.log(this.id)
  var id=this.id
  var that = this
  //查找有没有这个用户
  wx.request({
    url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/searchUser',
    method: 'POST',
    data:
    {
      token: app.globalData.userInfo.token,
      key: id
    },
    success: function (res) {
    console.log(res)
    if(res.data.list!='')
    {
    that.setData({
      list: res.data.list[0],
      modalName5:'friend'
    })
    }
    else{
      that.setData({
        notice:'notice',
        errmsg:"对不起,用户不存在"
      })
    }

  }
    })
},
  addPerson: function (e) {
    this.setData({
      modalName5: '',
    })
    console.log(e.currentTarget.dataset)
    console.log(this.data.info.id)
    var that=this
      //邀请盆友
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/inviteUser',
        method: 'POST',
        data:
        {
          token: app.globalData.userInfo.token,
          objectId: e.currentTarget.dataset.id * 1,
          roomId: that.data.info.id*1
        },
        success: function (res) {
          console.log(res)
          if(res.data.bool==true)
          {
          that.setData({
            notice:'notice',
            errmsg:"恭喜您,已经邀请"
          })
          }
          else{
            that.setData({
              notice: 'notice',
              errmsg: res.data.errmsg
            })
          }
        }
      })

  },
  setName:function(e)
  {
    console.log(e)
    this.data.NAME=e.detail.value
  },
  infoSubmit:function(e)
  {
    console.log("OK")
    var that=this
    console.log(that.data.info)
    console.log(that.data.id)
    console.log(that.data.NAME)
    console.log(that.data.bgimg)
    console.log(that.data.url)
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/updateChatRoom',
      method:'POST',
      data:{
        token: app.globalData.userInfo.token,
        chatRoom:
        {
          id: that.data.info.id*1,
          name:that.data.NAME,
          imgURL:that.data.url,
          bgURL:that.data.bgimg,
          creationTime:that.data.info.time,
          roomCreator:that.data.info.creatorid,
        }
      },
      success:function(e)
      {
        console.log(e)
        if(e.data.bool==true)
        {
          that.setData({
            notice:'notice',
            errmsg:"恭喜您更改成功"
          })
        }
      }

    })

    
  },
  goTomember:function(e)
  {
    var info = JSON.stringify(this.data.info)
    var type ="member"
    wx.navigateTo({
      url: '/pages/home/member/member?info=' + info+'&type='+type,
    })

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  changePic:function()
  {
    var that =this
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        app.globalData.backgroundPic = tempFilePaths[0]
        wx.uploadFile({
          url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/fileInfo/upload',
          filePath: tempFilePaths[0],
          header: {
            "Content-Type": "multipart/form-data"
          },
          name: 'myFile',
          success(res) {
            var data = JSON.parse(res.data)
            console.log(data)
            that.data.bgimg=data.url
        
          }
      })
      }
    })

  },
  goTohistory:function()
  {
    // //1代表查找历史
    // let type='room'
    // wx.navigateTo({
    //   url: '/pages/home/detail/detail?type='+type,
    // })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})